package controlerLayer;
import java.util.ArrayList;

import modelLayer.Sales;
import modelLayer.SalesContainer;
import modelLayer.Tree;

/**
 * The Controller Class for the Sales Objects
 * 
 * @author Nick D. Pedersen 
 * @version 0.1
 */
public class SalesCtr
{
    private SalesContainer sCont;

    /**
     * Constructor for objects of class SalesCtr
     */
    public SalesCtr()
    {
        sCont = SalesContainer.getInstance();
    }
    
    /**
     * createSales - Creates a Sales object
     * @Param String forestName - The name of the forest to buy from
     * @Param String treeSort - The name of the tree sort to buy
     * @Param double amount - The amount of trees, in Square Kilometers
     */
    public void createSales(String forestName, String treeSort, double amount){
        ForestCtr fCtr = new ForestCtr();
        Tree t = fCtr.findForest(forestName).findTree(treeSort);
        if(t == null){
            throw new NullPointerException("The Tree does not exists in that Forest!");
        }
        sCont.addSale(new Sales(t, amount));
    }
    
    /**
     * getSales - Returns a String with the information of all the sales
     * @Return String - a String that contains the information of all the sales
     */
    public String getSales(){
        String sales = "";
        String nLine = System.getProperty("line.separator");
        ArrayList<Sales> sList = sCont.getSales();
        if(!sList.isEmpty()){
            for(Sales s : sList){
                sales += s + nLine;
            }
        }else{
            throw new NullPointerException("There are no sales yet!");
        }
        
        return sales;
    }
    
    /**
     * commitSales - Makes a sale, removes the amounts from the trees, and returns a String of the sale
     * @Return String - A string that contains the information of the sale
     */
    public String commitSales(){
        String sales = "";
        String nLine = System.getProperty("line.separator");
        ArrayList<Sales> sList = sCont.getSales();
        if(!sList.isEmpty()){
            for(Sales s : sList){
                sales += s.commit() + nLine;
            }
            sCont.clearSales();
        }else{
            throw new NullPointerException("There are no sales to commit!");
        }
        
        return sales;
    }
    
    /**
     * commitSales - Makes a sale, removes the amounts from the trees, and returns a String of the sale
     * @Return String - A string that contains the information of the sale
     */
    public double getTotal(){
        double total = 0;
        ArrayList<Sales> sList = sCont.getSales();
        if(!sList.isEmpty()){
            for(Sales s : sList){
                total += s.getPrice();
            }
        }else{
            throw new NullPointerException("There are no sales to commit!");
        }
        
        return total;
    }
    
    /**
     * cancelSales - Clears the sales container of Sales Objects
     */
    public String cancelSales(){
        return sCont.cancelSales();
    }
}
